package kandl.automation.steprunner.selenium;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

import kandl.automation.Browser;
import kandl.automation.testsuite.TestProfile;

@Component
public class MacWebDriverFactory implements WebDriverFactory {

    @Value("${selenium.timeout.seconds}")
    private int timeout;

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }

    @Override
    public WebDriver getWebDriver(TestProfile profile) {
        Browser browser = profile.getBrowser();
        WebDriver driver;
        if (browser.getType().equals(Browser.FIREFOX_TYPE)) {
            driver = new FirefoxDriver();
        }
        else if (browser.getType().equals(Browser.CHROME_TYPE)){
            driver = new ChromeDriver();
        }
        else if (browser.getType().equals(Browser.SAFARI_TYPE)) {
            driver = new SafariDriver();
        }
        else {
            throw new UnsupportedOperationException("Cannot start a Webdriver for " + browser);
        }
        driver.manage().timeouts().implicitlyWait(timeout, TimeUnit.SECONDS);
        return driver;
    }
}
